<?php
// +----------------------------------------------------------------------
// | UBTEN.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
namespace User\Controller;
use User\Controller\BaseController;


class OrderController extends BaseController {

  //全部订单
    public function index(){
        $map['user_id'] = session('user_id');
        $map['delete_state'] = 0;
        $oModel = D('Order'); // 实例化User对象
        $count= $oModel->where($map)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $arrOrder = $oModel->where($map)->relation('OrderGoods')->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);
        $this->assign('arrOrder',$arrOrder);
        $this->assign('active','order');
        $this->display();
        
    }
    
    
    //待付款
    public function waitPay(){
        $map['order_state'] = 10;
        $map['delete_state'] = 0;
        $map['user_id'] = session('user_id');
        $oModel = D('Order'); // 实例化User对象
        $count= $oModel->where($map)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $arrOrder = $oModel->where($map)->relation('OrderGoods')->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);
        $this->assign('arrOrder',$arrOrder);
        $this->assign('active','order');
        $this->display();
    }
    
    
    //待发货
    public function waitSend(){
        $oModel = D('Order');
        $map['user_id'] = session('user_id');
        $map['order_state'] = 20;
        $count= $oModel->where($map)->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $arrOrder = $oModel->where($map)->relation('OrderGoods')->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);
        $this->assign('arrOrder',$arrOrder);
        $this->assign('active','order');
        $this->display();
    }
    
    
    //待收货
    public function waitConfirm(){
        $oModel = D('Order');
        $map['user_id'] = session('user_id');
        $map['order_state'] = 30;
        $count= $oModel->where($map)->group('pay_sn')->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $arrOrder = $oModel->where($map)->relation('OrderGoods')->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);
        $this->assign('arrOrder',$arrOrder);
        $this->assign('active','order');
        $this->display();
    }
    
    
    //待评价
    public function waitRate(){
        $oModel = D('Order');
        $map['user_id'] = session('user_id');
        $map['order_state'] = 40;
//        $map['evaluation_state'] = 0;
        $count= $oModel->where($map)->group('pay_sn')->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $arrOrder = $oModel->where($map)->relation('OrderGoods')->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);
        $this->assign('arrOrder',$arrOrder);
        $this->assign('active','order');
        $this->display();
    }

        //订单详情
    public function details(){
        $orderID = I('key');
        $oModel = D('Order');
        $map['order_sn'] = $orderID;
        $detOrder = $oModel->where($map)->relation('OrderGoods')->select();
        foreach ($detOrder as $key => $value) {
            $detOrder[$key]['OrderCommon'] = M('OrderCommon')->where('order_id='.$value['order_id'])->find();
            $detOrder[$key]['address'] = unserialize($detOrder[$key]['OrderCommon']['reciver_info']);
        }

        $this->assign('detOrder',$detOrder);
        $this->assign('active','order');
        $this->display();
    }

    //确认收货
    public function confirm(){
        $data['buyer_id'] = session('user_id');
        $data['order_id'] = I('id');
        $order = M('order')->where($data)->save(array('order_state'=>40));
        $order ? $this->success('操作成功', U('index')) : $this->error('操作失败');
    }

    //取消订单
    public function cancel(){
        $data['user_id'] = session('user_id');
        $data['order_id'] = I('post.order_id');
        $order = M('Order')->where($data)->save(array('order_state'=>0));
        $order ? $this->success('操作成功', U('index')) : $this->error('操作失败');

    }

    //提醒发货
    public function notice(){
        $this->success('提醒成功');
    }

    //删除订单
    public function del() {
        $where['order_id'] = I("post.order_id");
        $where['user_id'] = session('user_id');
        $order = M("Order")->where($where)->save(array('delete_state'=>1));
        $order ? $this->success('操作成功', U('index')) : $this->error('操作失败');
    }

    public function xb() {
        //计算出多订单的相关支付的价格（除去优惠券实际支付的价格）
        $map['pay_sn'] = "1611301497524569";
        $oModel = D('Order');
        $oList = $oModel->relation('OrderGoods')->where($map)->select();
        
        //获取平台的优惠券的信息
        $map['voucher_store'] = 0;
        $tddList = M('order_voucher')->where($map)->select();
        if (!empty($tddList)) {
            foreach ($tddList as $key => $value) {
                $totalTdd += $value['voucher_price'];
            }
        }
        $bili = (1 - ($notify->data['cash_fee'] / 100) / $totalTdd); //平台优惠比例

        foreach ($oList as $key => $value) {
            if ($value['discount_fee'] > 0) {
                foreach ($value['OrderGoods'] as $k => $v) {
                    $pay_price[$k] = $bili * $v['goods_price'];
                    $trader_price += $bili * $v['goods_price'];
                }
                $bili2 = (1 - ($trader_price / $value['discount_fee']));
                //根据占比更新单个商品时间支付的价格
                foreach ($pay_price as $k => $v) {
                    //更新实际支付的价格
                    $pay[$k] = $bili2 * $v;
                    //在根据店铺的优惠比例继续计算实际支付的价格
                    M('OrderGoods')->where(array('order_id' => $v['order_id'], 'goods_id' => $v['goods_id']))->save(array('goods_pay_price' => $pay[$k]));
                    $total_price[$key] += $pay_price[$k];  //计算出订单的总价格
                }
                //更新订单的价格及状态
                //M("order")->where(array('order_id' => $value['order_id']))->save(array('order_state' => 20, 'order_amount' => $total_price[$key], 'payment_time' => time()));
            } else {
                //根据占比更新单个商品时间支付的价格
                foreach ($value['OrderGoods'] as $k => $v) {
                    //更新实际支付的价格
                    $pay_price[$k] = $bili * $v['goods_price'];
                    M('OrderGoods')->where(array('order_id' => $v['order_id'], 'goods_id' => $v['goods_id']))->save(array('goods_pay_price' => $pay_price[$k]));
                    $total_price[$key] += $pay_price[$k];  //计算出订单的总价格
                }
                //更新订单的价格及状态
                //M("order")->where(array('order_id' => $value['order_id']))->save(array('order_state' => 20, 'order_amount' => $total_price[$key], 'payment_time' => time()));
            }
        }
    }
    

    //快递信息
    public function express(){
        
        $key = I('key');
        $map['order_id'] = $key;
        $oModel = D('Order');
        $arrOrder = $oModel->where($map)->relation('OrderCommon')->find();
        $arrOrder['address'] = unserialize($arrOrder['OrderCommon']['reciver_info']);
        $arrOrder['OrderGoods'] = M('order_goods')->where('order_id='.$arrOrder['order_id'])->select();
        $this->assign('arrOrder', $arrOrder);
        $arr = json_decode(kuaidi($arrOrder['express_id'], $arrOrder['shipping_code']));
        $this->assign('arr', $arr);
        $this->assign('active','order');
        $this->display();
    }

}
